package fault_file

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/fault_file"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    fault_fileReq "github.com/flipped-aurora/gin-vue-admin/server/model/fault_file/request"
)

type Fault_file_structService struct {
}

// CreateFault_file_struct 创建Fault_file_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (fault_file_structService *Fault_file_structService) CreateFault_file_struct(fault_file_struct *fault_file.Fault_file_struct) (err error) {
	err = global.GVA_DB.Create(fault_file_struct).Error
	return err
}

// DeleteFault_file_struct 删除Fault_file_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (fault_file_structService *Fault_file_structService)DeleteFault_file_struct(fault_file_struct fault_file.Fault_file_struct) (err error) {
	err = global.GVA_DB.Delete(&fault_file_struct).Error
	return err
}

// DeleteFault_file_structByIds 批量删除Fault_file_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (fault_file_structService *Fault_file_structService)DeleteFault_file_structByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]fault_file.Fault_file_struct{},"id in ?",ids.Ids).Error
	return err
}

// UpdateFault_file_struct 更新Fault_file_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (fault_file_structService *Fault_file_structService)UpdateFault_file_struct(fault_file_struct fault_file.Fault_file_struct) (err error) {
	err = global.GVA_DB.Save(&fault_file_struct).Error
	return err
}

// GetFault_file_struct 根据id获取Fault_file_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (fault_file_structService *Fault_file_structService)GetFault_file_struct(id uint) (fault_file_struct fault_file.Fault_file_struct, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&fault_file_struct).Error
	return
}

// GetFault_file_structInfoList 分页获取Fault_file_struct记录
// Author [piexlmax](https://github.com/piexlmax)
func (fault_file_structService *Fault_file_structService)GetFault_file_structInfoList(info fault_fileReq.Fault_file_structSearch) (list []fault_file.Fault_file_struct, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&fault_file.Fault_file_struct{})
    var fault_file_structs []fault_file.Fault_file_struct
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Ata_chapter_level1 != "" {
        db = db.Where("ata_chapter_level1 LIKE ?","%"+ info.Ata_chapter_level1+"%")
    }
    if info.Ata_chapter_level2 != "" {
        db = db.Where("ata_chapter_level2 LIKE ?","%"+ info.Ata_chapter_level2+"%")
    }
    if info.Fault_number != "" {
        db = db.Where("fault_number LIKE ?","%"+ info.Fault_number+"%")
    }
    if info.Fault_name != "" {
        db = db.Where("fault_name LIKE ?","%"+ info.Fault_name+"%")
    }
    if info.Ao_number != "" {
        db = db.Where("ao_number LIKE ?","%"+ info.Ao_number+"%")
    }
    if info.Sorties != "" {
        db = db.Where("sorties LIKE ?","%"+ info.Sorties+"%")
    }
        if info.StartTroubleshooting_time != nil && info.EndTroubleshooting_time != nil {
            db = db.Where("troubleshooting_time BETWEEN ? AND ? ",info.StartTroubleshooting_time,info.EndTroubleshooting_time)
        }
    if info.Drawing_number != "" {
        db = db.Where("drawing_number LIKE ?","%"+ info.Drawing_number+"%")
    }
    if info.Register_person != "" {
        db = db.Where("register_person LIKE ?","%"+ info.Register_person+"%")
    }
    if info.Operation_and_response_to_design_requirements != "" {
        db = db.Where("operation_and_response_to_design_requirements LIKE ?","%"+ info.Operation_and_response_to_design_requirements+"%")
    }
    if info.Symptoms != "" {
        db = db.Where("symptoms LIKE ?","%"+ info.Symptoms+"%")
    }
    if info.Fault_type != "" {
        db = db.Where("fault_type LIKE ?","%"+ info.Fault_type+"%")
    }
    if info.Conclusion != "" {
        db = db.Where("conclusion LIKE ?","%"+ info.Conclusion+"%")
    }
    if info.References != "" {
        db = db.Where("references LIKE ?","%"+ info.References+"%")
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
        var OrderStr string
        orderMap := make(map[string]bool)
         	orderMap["troubleshooting_time"] = true
       if orderMap[info.Sort] {
          OrderStr = info.Sort
          if info.Order == "descending" {
             OrderStr = OrderStr + " desc"
          }
          db = db.Order(OrderStr)
       }

	err = db.Limit(limit).Offset(offset).Find(&fault_file_structs).Error
	return  fault_file_structs, total, err
}
